package com.lkj.youmeng.mvp.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;
import com.lkj.youmeng.R;
import com.lkj.youmeng.adapter.PagerAdapter;
import com.lkj.youmeng.base.BaseFragment;
import com.lkj.youmeng.utils.LogUtils;

import java.util.ArrayList;
import java.util.List;

public class ContentFragment extends BaseFragment {
    private TabLayout mTabLayout;
    private ViewPager mViewPager;
    private String[] titles = {"推荐","圈子", "同城"};
    private ArrayList<String> mTitle = new ArrayList<>();
    private List<BaseFragment> mFragmentList = new ArrayList<>();
    private PagerAdapter mAdapter;
    private int type = 1; //1.推荐，2.圈子，3.同城

    public static ContentFragment newInstance(Bundle bundle){
        ContentFragment fragment = new ContentFragment();
        if (bundle != null){
            fragment.setArguments(bundle);
        }
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_content, container,false);
        initView(view);
        initTab();
        initFragment();
        initViewPager();
        return view;
    }

    private void initViewPager() {
        mAdapter = new PagerAdapter(getActivity().getSupportFragmentManager(),mFragmentList,mTitle);
        mViewPager.setAdapter(mAdapter);
        mViewPager.setOffscreenPageLimit(3);
        //为ViewPager添加页面改变监听
        mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(mTabLayout));
    }

    private void initFragment() {
        mFragmentList.clear();
        Bundle bundle = new Bundle();
        bundle.putInt("type",1);
        RecommendFragment fragment = RecommendFragment.newInstance(bundle);
        mFragmentList.add(fragment);

        Bundle bundle2 = new Bundle();
        bundle.putInt("type",2);
        FriendFragment fragment2 = FriendFragment.newInstance(bundle2);
        mFragmentList.add(fragment2);

        Bundle bundle3 = new Bundle();
        bundle.putInt("type",3);
        CityFragment fragment3 = CityFragment.newInstance(bundle3);
        mFragmentList.add(fragment3);
        LogUtils.d(  ",mFragmentList -->" + mFragmentList.size());
    }

    private void initTab() {
        mTabLayout.setTabMode(TabLayout.MODE_FIXED);
        for (int i = 0 ; i<titles.length;i++){
            mTitle.add(titles[i]);
            mTabLayout.addTab(mTabLayout.newTab().setText(titles[i]));
        }
        mTabLayout.setupWithViewPager(mViewPager);
    }

    private void initView(View view) {
        mTabLayout =  view.findViewById(R.id.tl_tab);
        mViewPager = view.findViewById(R.id.vp_content);
    }

}
